package com.jzqf.harmonydemo.library.database.helper;

import com.jzqf.harmonydemo.library.database.DatabaseManager;
import com.jzqf.harmonydemo.library.database.entity.UserEntity;
import ohos.data.orm.OrmPredicates;

import java.util.List;

public class UserHelper {

    public static void insert(UserEntity entity) {
        DatabaseManager.getOrmContext().insert(entity);
        DatabaseManager.getOrmContext().flush();
    }

    public static void inserts(List<UserEntity> list) {
        DatabaseManager.getOrmContext().beginTransaction();
        for (UserEntity entity : list) {
            DatabaseManager.getOrmContext().insert(entity);
        }
        DatabaseManager.getOrmContext().commit();
    }

    public static List<UserEntity> queryAll() {
        OrmPredicates predicates = DatabaseManager.getOrmContext().where(UserEntity.class);
        return DatabaseManager.getOrmContext().query(predicates);
    }

    public static void update(UserEntity entity) {
        DatabaseManager.getOrmContext().update(entity);
        DatabaseManager.getOrmContext().flush();
    }

    public static void deleteByName(String userName) {
        OrmPredicates predicates = DatabaseManager.getOrmContext().where(UserEntity.class);
        predicates.equalTo("userName", userName);
        DatabaseManager.getOrmContext().delete(predicates);
        DatabaseManager.getOrmContext().flush();
    }

    public static void deleteAll() {
        OrmPredicates predicates = DatabaseManager.getOrmContext().where(UserEntity.class);
        DatabaseManager.getOrmContext().delete(predicates);
        DatabaseManager.getOrmContext().flush();
    }

}
